﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlgorithmDemo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        #region 属性

        #endregion

        #region 事件
        /// <summary>
        /// 计算Fibonacci
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnCalcFibonacci_Click(object sender, EventArgs e)
        {
            //输出Fibonacci数列的前20项
            //开始循环
            for (int i = 1; i <= 20; i++)
            {
                rtxtFibonacci.Text = rtxtFibonacci.Text + Fibonacci(i).ToString() + "\n";
            }
        }

        private void btnCalcHundredChicken_Click(object sender, EventArgs e)
        {
            int Cock, Hen, Chick;
            //For循环版本百钱百鸡
            for (Cock = 0; Cock < 19; Cock++)   //因为公鸡最大不会超过19只,所以最多循环19次.
            {
                for (Hen = 0; Hen < 33; Hen++)  //母鸡最大也不会超过33次,所以最多循环33次
                {
                    Chick = 100 - Cock - Hen;  //小鸡等于100 - 公鸡个数 -  母鸡个数
                    if (Cock * 5 + Hen * 3 + Chick / 3 == 100)  // 判断三种鸡乘以单价的总数是否等于100元
                    {
                        rtxtCalcHundredChicken.Text = rtxtCalcHundredChicken.Text + "公鸡有" + Cock + "只，母鸡有" + Hen + "只，小鸡有" + Chick + "只\n";
                    }
                }
            }

        }


        #endregion
        #region 方法
        /// <summary>
        /// 递归法，输出Fibonacci数列
        /// </summary>
        /// <param name="n">输出数列的第几项</param>
        /// <returns>输出的Fibonacci数列项</returns>
        private static int Fibonacci(int n)
        {
            if (n < 3)
                return 1;
            else
                return Fibonacci(n - 1) + Fibonacci(n - 2);
        }
        #endregion
    }
}
